<template>
  <div>
    <Row>
      <Col span="24">
        <Card>
          <div class="margin-bottom-15">
            <Button type="primary" v-has="'Mail/add'" @click="alertAdd" icon="md-add">{{ $t('add_button') }}</Button>
          </div>
          <div>
            <Table :loading="listLoading" :columns="columnsList" :data="tableData" border disabled-hover />
          </div>
          <div class="margin-top-15" style="text-align: center">
            <Page :total="tableShow.listCount" :current="tableShow.currentPage"
                  :page-size="tableShow.pageSize" @on-change="changePage"
                  @on-page-size-change="changeSize" show-elevator show-sizer show-total />
          </div>
        </Card>
      </Col>
    </Row>
    <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
      <p slot="header" style="color:#2d8cf0">
        <Icon type="md-alert" />
        <span>{{formItem.id ? '编辑' : '新增'}}公告</span>
      </p>
      <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="100">
        <FormItem label="公告内容" prop="proclamation">
          <Input v-model="formItem.proclamation" type="textarea" :autosize="{minRows: 5,maxRows: 20}" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel" class="margin-right-10">取消</Button>
        <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
      </div>
    </Modal>

  </div>
</template>
<script>
import { getList, add, edit, del } from '@/api/game-proclamation'
const editButton = (vm, h, currentRow, index) => {
  if (vm.buttonShow.edit) {
    return h('Button', {
      props: {
        type: 'primary'
      },
      style: {
        margin: '0 5px'
      },
      on: {
        'click': () => {
          vm.formItem.proclamation = currentRow.proclamation
          vm.formItem.id = currentRow.id
          vm.alertAdd()
        }
      }
    }, '编辑')
  }
}
const deleteButton = (vm, h, currentRow, index) => {
  if (vm.buttonShow.del) {
    return h('Poptip', {
      props: {
        confirm: true,
        title: '您确定要删除这条数据吗? ',
        transfer: true
      },
      on: {
        'on-ok': () => {
          del(currentRow.id).then(response => {
            vm.tableData.splice(index, 1)
            vm.$Message.success(response.data.msg)
          })
          currentRow.loading = false
        }
      }
    }, [
      h('Button', {
        style: {
          margin: '0 5px'
        },
        props: {
          type: 'error',
          placement: 'top',
          loading: currentRow.isDeleting
        }
      }, vm.$t('delete_button'))
    ])
  }
}
export default {
  name: 'operate-mail',
  data () {
    return {
      typeList: [],
      nameLoading: false,
      gameNameOption: [],
      gameShow: {},
      columnsList: [
        {
          title: '序号',
          key: 'id',
          width: 65,
          align: 'center'
        },
        {
          title: '公告内容',
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.proclamation.slice(0, 60) + '...')
          }
        },
        {
          title: '发布时间',
          align: 'center',
          key: 'created_at',
          width: 200
        },
        {
          title: '操作',
          align: 'center',
          width: 200,
          render: (h, params) => {
            return h('div', [
              editButton(this, h, params.row, params.index),
              deleteButton(this, h, params.row, params.index)
            ])
          }
        }
      ],
      tableData: [],
      tableShow: {
        currentPage: 1,
        pageSize: 10,
        listCount: 0
      },
      searchConf: {
        game_hash: ''
      },
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      formItem: {
        proclamation: '',
        id: 0
      },
      ruleValidate: {
        proclamation: [
          { required: true, message: '输入公告内容', trigger: 'blur' }
        ]
      },
      buttonShow: {
        edit: true,
        del: true
      },
      keyColumnsList: [
        {
          title: '序号',
          type: 'index',
          width: 65,
          align: 'center'
        },
        {
          title: '所属游戏',
          align: 'center',
          render: (h, params) => {
            return h('span', this.gameShow[params.row.game_hash])
          },
          width: 200
        },
        {
          title: '使用者',
          align: 'center',
          key: 'openid',
          render: (h, params) => {
            if (params.row.openid) {
              return h('span', params.row.openid)
            } else {
              return h('span', '-')
            }
          },
          minWidth: 260
        },
        {
          title: '使用角色ID',
          align: 'center',
          key: 'role_id',
          render: (h, params) => {
            if (params.row.role_id) {
              return h('span', params.row.role_id)
            } else {
              return h('span', '-')
            }
          },
          minWidth: 160
        },
        {
          title: '邮件名称',
          align: 'center',
          key: 'title',
          tooltip: true,
          width: 150
        },
        {
          title: '邮件附件',
          align: 'center',
          key: 'reward',
          tooltip: true,
          minWidth: 120
        },
        {
          title: '邮件状态',
          align: 'center',
          width: 130,
          render: (h, params) => {
            if (params.row.status === 1) {
              return h('span', '未领取')
            } else if (params.row.status === 4) {
              return h('span', '已失效')
            } else {
              return h('span', '已领取')
            }
          }
        },
        {
          title: '兑换时间',
          align: 'center',
          render: (h, params) => {
            if (params.row.status === '1') {
              return h('span', '未领取')
            } else if (params.row.status === '4') {
              return h('span', '已失效')
            } else {
              return h('span', params.row.updated_at)
            }
          },
          width: 200
        }
      ],
      keyData: [],
      keyShow: {
        currentPage: 1,
        pageSize: 10,
        listCount: 0
      },
      listLoading: false
    }
  },
  created () {
    let vm = this
    vm.getList()
  },
  methods: {
    alertAdd () {
      this.modalSetting.show = true
      this.modalSetting.index = 0
    },
    changeKeyPage (page) {
      this.keyShow.currentPage = page
      this.getKeyList()
    },
    changeKeySize (size) {
      this.keyShow.pageSize = size
      this.getKeyList()
    },
    submit () {
      let vm = this
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          vm.modalSetting.loading = true
          if (vm.formItem.id === 0) {
            add(vm.formItem).then(response => {
              vm.$Message.success(response.data.msg)
              vm.getList()
              vm.cancel()
            }).catch(() => {
              vm.modalSetting.loading = false
            })
          } else {
            edit(vm.formItem).then(response => {
              vm.$Message.success(response.data.msg)
              vm.getList()
              vm.cancel()
            }).catch(() => {
              vm.modalSetting.loading = false
            })
          }
        }
      })
    },
    cancel () {
      this.modalSetting.show = false
    },
    doCancel (data) {
      if (!data) {
        this.formItem.id = 0
        this.$refs['myForm'].resetFields()
        this.modalSetting.loading = false
        this.modalSetting.index = 0
      }
    },
    changePage (page) {
      this.tableShow.currentPage = page
      this.getList()
    },
    changeSize (size) {
      this.tableShow.pageSize = size
      this.getList()
    },
    search () {
      this.tableShow.currentPage = 1
      this.getList()
    },
    getList () {
      let vm = this
      let params = {
        page: vm.tableShow.currentPage,
        size: vm.tableShow.pageSize
      }
      vm.listLoading = true
      getList(params).then(response => {
        vm.tableData = response.data.data.list
        vm.tableShow.listCount = response.data.data.count
        vm.listLoading = false
      })
    }
  }
}
</script>
